<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class goods extends core {
	public $goods_mod;

	public function __construct() {
		parent::__construct();
		$this->goods_mod = m('goods');
	}

	//列表
	public function index() {
		$where = '';
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
        $shop_id = (isset($_GET['shop_id']) && trim($_GET['shop_id'])) ? intval($_GET['shop_id']) : 0;
        $cate_id = (isset($_GET['cate_id']) && trim($_GET['cate_id'])) ? intval($_GET['cate_id']) : 0;
        $brand_id = (isset($_GET['brand_id']) && trim($_GET['brand_id'])) ? intval($_GET['brand_id']) : 0;
        $startprice = (isset($_GET['startprice']) && trim($_GET['startprice'])) ? trim($_GET['startprice']) : '';
        $endprice = (isset($_GET['endprice']) && trim($_GET['endprice'])) ? trim($_GET['endprice']) : '';
        $jiage = (isset($_GET['jiage']) && trim($_GET['jiage'])) ? intval($_GET['jiage']) : 0;
        $ext_property = isset($_GET['ext_property']) ? $_GET['ext_property'] : '';
        $oldwhere = (isset($_GET['oldwhere']) && trim($_GET['oldwhere'])) ? trim($_GET['oldwhere']) : '';

        if ($id) {
            $where .= " AND g.id='{$id}'";
        }
        if ($keyword) {
            $where .= " AND (g.name LIKE '%{$keyword}%' OR g.description LIKE '%{$keyword}%' OR g.content LIKE '%{$keyword}%')";
        }
        if ($shop_id) {
			$where .= " AND g.shop_id='{$shop_id}'";
		}
		if ($cate_id) {
			$where .= " AND g.category_id = '{$cate_id}'";
		}
		if ($brand_id) {
			$where .= " AND g.brand_id = '{$brand_id}'";
		}
		if ($startprice >= 0) {
			$where .= " AND (g.price >= '{$startprice}')";
		}
		if($jiage > 0){
			if ($endprice >= 0) $where .= " AND (g.price <= '{$endprice}')";
		}
		if ($ext_property) {
			if (!is_array($ext_property)) $ext_property = explode(',', $ext_property);
			$where .= " AND (";
			foreach ($ext_property as $e) {
				$where .= "CONCAT(',',g.ext_property,',') LIKE '%,{$e},%' OR ";
			}
			$where = trim($where, ' OR ').")";
			$ext_property = implode(',',$ext_property);
		}
		$oldwhere = urlencode("keyword={$keyword}&shop_id={$shop_id}&startprice={$startprice}&endprice={$endprice}&cate_id={$cate_id}&brand_id={$brand_id}&ext_property={$ext_property}");
        //分页
        $this->setpages(array('id'=>$id, 'keyword'=>$keyword, 'shop_id'=>$shop_id, 'cate_id'=>$cate_id, 'brand_id'=>$brand_id,
        	'startprice'=>$startprice, 'endprice'=>$endprice, 'ext_property'=>$ext_property));

		//$this->ezr->num_results_per_page = 100;
		$sql = "SELECT g.* ,c.name as cname ,b.name as bname ,s.name as sname
			FROM {$this->tbp}goods g
			LEFT JOIN {$this->tbp}goods_category c ON g.category_id =c.id
			LEFT JOIN {$this->tbp}brand b ON g.brand_id = b.id
			LEFT JOIN {$this->tbp}shop s ON g.shop_id = s.id
			WHERE 1 {$where} ORDER BY g.id DESC";
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		if ($rs) {
			foreach ($rs as $k => $g) {
				$goods_id = $g['id'];
				$url = "{$this->domain}/wap.php?app=goods&act=detail&id={$goods_id}&qrcode=1";
				$url = urlencode($url);
				$rs[$k]['url'] = $url;
			}
		}
		$rs = add_domain_deep($rs, array('pic'));

		$sql = "SELECT id,name FROM {$this->tbp}shop ORDER BY id ASC";
		$shops = $this->db->get_results($sql);
		$this->smarty->assign('shops',$shops);
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('shop_id',$shop_id);
		$this->smarty->assign('cate_id',$cate_id);
		$this->smarty->assign('brand_id',$brand_id);
		$this->smarty->assign('startprice',$startprice);
		$this->smarty->assign('endprice',$endprice);
		$this->smarty->assign('ext_property',$ext_property);
		$this->smarty->assign('oldwhere',$oldwhere);
		//品牌
		//$sql = "SELECT name ,id FROM {$this->tbp}brand WHERE status = 1 ORDER BY sort ASC, id ASC";
		//$brand = $this->db->get_results($sql);
		//$this->smarty->assign('brand',$brand);
		//分类
		$categorys = $this->goods_mod->get_categorys();
		$this->smarty->assign('categorys', $this->goods_mod->set_categorys_option($categorys, $cate_id));
		$this->display();
	}

	//省份
    public function province() {
        $where = '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
        if ($keyword) {
            $where .= "name LIKE '%{$keyword}%'";
        }

        //分页
        $this->setpages(array('keyword'=>$keyword));

        $rs = SQL::share('service_city s')->left('city c', 's.city_id=c.city_id')->isezr()->where($where)
            ->find('s.id, s.city_id, s.parent_id, c.name');

        if ($rs){
            foreach ($rs as $k => $g){
                $rs[$k]->province_id = SQL::share('service_province')->where("id='{$g->parent_id}'")->val('province_id');
            }
        }

       // var_dump($rs);exit();
        $nav = $this->ezr->get_navigation();

        $this->smarty->assign('rs',$rs);
        $this->smarty->assign('page',$nav);

        $this->display();

    }

	
	//add
	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			//店铺
			//$sql = "SELECT name ,id FROM {$this->tbp}shop WHERE 1";
			//$shop = $this->db->get_results($sql);
			//$this->smarty->assign('shop',$shop);
			//品牌
			//$sql = "SELECT name ,id FROM {$this->tbp}brand WHERE status = 1";
			//$brand = $this->db->get_results($sql);
			//$this->smarty->assign('brand',$brand);
			//分类
			$categorys = $this->goods_mod->get_categorys();
			$this->smarty->assign('categorys', $this->goods_mod->set_categorys_option($categorys));
			
			$this->display();
		}
	}

	//编辑
	public function edit() {
		if (IS_POST) {
			$this->save();
		} else {
			//商品
			$id = (isset($_GET['id']) && intval($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM {$this->tbp}goods WHERE id='{$id}'";
			$row = $this->db->get_row($sql);
			$this->smarty->assign('row', $row);
			//店铺
			//$sql = "SELECT id, name FROM {$this->tbp}shop WHERE 1";
			//$shop = $this->db->get_results($sql);
			//$this->smarty->assign('shop',$shop);
			//品牌
			//$sql = "SELECT name ,id FROM {$this->tbp}brand WHERE status = 1";
			//$brand = $this->db->get_results($sql);
			//$this->smarty->assign('brand',$brand);
			//分类
			$categorys = $this->goods_mod->get_categorys();
			$this->smarty->assign('categorys', $this->goods_mod->set_categorys_option($categorys, $row->category_id));
			//商品图
			$sql = "SELECT * FROM {$this->tbp}goods_pic WHERE goods_id = '{$id}'";
			$pics = $this->db->get_results($sql);
			$pics = add_domain_deep($pics, array("pic"));
			$this->smarty->assign('pics', $pics);
			//规格
			$sql = "SELECT * FROM {$this->tbp}goods_spec WHERE goods_id = '{$id}'";
			$spec = $this->db->get_results($sql);
			$this->smarty->assign('spec', $spec);
			$oldwhere = (isset($_GET['oldwhere']) && trim($_GET['oldwhere'])) ? trim($_GET['oldwhere']) : '';
			//在列表页第几页进入编辑
			$current_page = (isset($_GET['current_page']) && trim($_GET['current_page'])) ? intval($_GET['current_page']) : 0;
			$this->smarty->assign('oldwhere', $oldwhere);
			$this->smarty->assign('current_page', $current_page);
			$this->display();
		}
	}
	public function upload_pic(){
        $result = upload_one_file('goods', 'pic');
        echo $result;
    }
	
	//保存
	public function save() {
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
		$description = (isset($_POST['description']) && trim($_POST['description'])) ? trim($_POST['description']) : '';
		$keywords = (isset($_POST['keywords']) && trim($_POST['keywords'])) ? trim($_POST['keywords']) : '';
		$content = (isset($_POST['content']) && trim($_POST['content'])) ? trim($_POST['content']) : '';
		$weight = (isset($_POST['weight']) && trim($_POST['weight'])) ? trim($_POST['weight']) : '';
		$size = (isset($_POST['size']) && trim($_POST['size'])) ? trim($_POST['size']) : '';
		$price = (isset($_POST['price']) && trim($_POST['price'])) ? trim($_POST['price']) : '';
		$market_price = (isset($_POST['market_price']) && trim($_POST['market_price'])) ? trim($_POST['market_price']) : '';
		$cost_price = (isset($_POST['cost_price']) && trim($_POST['cost_price'])) ? trim($_POST['cost_price']) : '';
		$promote_price = (isset($_POST['promote_price']) && trim($_POST['promote_price'])) ? trim($_POST['promote_price']) : '';
		$promote_begin_time = (isset($_POST['begin_time']) && trim($_POST['begin_time'])) ? trim($_POST['begin_time']) : '';
		$promote_end_time = (isset($_POST['end_time']) && trim($_POST['end_time'])) ? trim($_POST['end_time']) : '';
		$integral = (isset($_POST['integral']) && intval($_POST['integral'])) ? intval($_POST['integral']) : 0;
		$give_integral = (isset($_POST['give_integral']) && intval($_POST['give_integral'])) ? intval($_POST['give_integral']) : 0;
		$category_id = (isset($_POST['cate']) && intval($_POST['cate'])) ? intval($_POST['cate']) : 0;
		$shop_id = (isset($_POST['shop']) && intval($_POST['shop'])) ? intval($_POST['shop']) : 0;
		$brand_id = (isset($_POST['brand']) && intval($_POST['brand'])) ? intval($_POST['brand']) : 0;
		$country_id = (isset($_POST['country']) && intval($_POST['country'])) ? intval($_POST['country']) : 0;
		$stocks = (isset($_POST['stocks']) && intval($_POST['stocks'])) ? intval($_POST['stocks']) : 0;
		$stock_alert_number = (isset($_POST['stocks_dg']) && intval($_POST['stocks_dg'])) ? intval($_POST['stocks_dg']) : 0;
		$ext_property = isset($_POST['ext_property']) ? $_POST['ext_property'] : '';
		$shipping_fee = (isset($_POST['fee']) && trim($_POST['fee'])) ? trim($_POST['fee']) : '';
		$free_shipping = (isset($_POST['free']) && intval($_POST['free'])) ? intval($_POST['free']) : 0;
		$commission_template_id = (isset($_POST['com_id']) && intval($_POST['com_id'])) ? intval($_POST['com_id']) : 0;
		$commissions = (isset($_POST['com']) && trim($_POST['com'])) ? trim($_POST['com']) : '';
		$sort = (isset($_POST['sort']) && intval($_POST['sort'])) ? intval($_POST['sort']) : 0;
		$status = (isset($_POST['status']) && intval($_POST['status'])) ? intval($_POST['status']) : 0;
		$seller_note = (isset($_POST['note']) && trim($_POST['note'])) ? trim($_POST['note']) : '';
		$in_shop = (isset($_POST['in_shop']) && intval($_POST['in_shop'])) ? intval($_POST['in_shop']) : 0;
		$sale_method = (isset($_POST['sale_method']) && intval($_POST['sale_method'])) ? intval($_POST['sale_method']) : 0;
		$purchase_count = (isset($_POST['purchase_count']) && intval($_POST['purchase_count'])) ? intval($_POST['purchase_count']) : 0;
		$pics = isset($_POST['pics']) ? $_POST['pics']: '';
		$videos = isset($_POST['videos']) ? $_POST['videos']: '';
		$memos = isset($_POST['memos']) ? $_POST['memos']: '';
		$spec = isset($_POST['spec']) ? $_POST['spec'] : '' ;
		$sto = isset($_POST['sto']) ? $_POST['sto'] : '' ;
		$pri = isset($_POST['pri']) ? $_POST['pri'] : '' ;
		$pro = isset($_POST['pro']) ? $_POST['pro'] : '' ;
		$oldwhere = (isset($_POST['oldwhere']) && trim($_POST['oldwhere'])) ? trim($_POST['oldwhere']) : '';
		$BRSR = (isset($_POST['current_page']) && trim($_POST['current_page'])) ? trim($_POST['current_page']) : '';
		if ($promote_begin_time) $promote_begin_time=strtotime($promote_begin_time);
		if ($promote_end_time) $promote_end_time=strtotime($promote_end_time);
		
		//图片
		$pic = upload_one_file('goods', 'pic');
		if ($free_shipping == 1) $shipping_fee = '';
		if (is_array($ext_property)) $ext_property = implode(',', $ext_property);
		
		if ($id>0) {
			$sql = "UPDATE {$this->tbp}goods SET name='{$name}',description='{$description}',keywords='{$keywords}',content='{$content}',
				weight='{$weight}',size='{$size}',price='{$price}',market_price='{$market_price}',cost_price='{$cost_price}',promote_price='{$promote_price}',
				pic='{$pic}',promote_begin_time='{$promote_begin_time}',promote_end_time='{$promote_end_time}',integral='{$integral}',
				give_integral='{$give_integral}',category_id='{$category_id}',shop_id='{$shop_id}',brand_id='{$brand_id}',
				country_id='{$country_id}',stocks='{$stocks}',stock_alert_number='{$stock_alert_number}',ext_property='{$ext_property}',
				shipping_fee='{$shipping_fee}',free_shipping='{$free_shipping}',commission_template_id='{$commission_template_id}',
				commissions='{$commissions}',sort='{$sort}',status='{$status}',seller_note='{$seller_note}',in_shop='{$in_shop}',
				purchase_count='{$purchase_count}',sale_method='{$sale_method}',edit_time='".time()."'
				WHERE id='{$id}'";
			$this->db->query($sql);
		} else {
			$sql = "INSERT INTO {$this->tbp}goods(name,description ,keywords,content,weight,size,price,market_price,cost_price,promote_price,
				promote_begin_time,promote_end_time,integral,give_integral,category_id,shop_id,brand_id,country_id,
				stocks,stock_alert_number,ext_property,shipping_fee,free_shipping,commission_template_id,commissions,
				sort,status,seller_note,in_shop,purchase_count,sale_method,add_time,pic)
				VALUES('{$name}', '{$description}', '{$keywords}', '{$content}', '{$weight}','{$size}','{$price}',
				'{$market_price}','{$cost_price}', '{$promote_price}', '{$promote_begin_time}' , '{$promote_end_time}',
				'{$integral}','{$give_integral}','{$category_id}','{$shop_id}','{$brand_id}','{$country_id}',
				'{$stocks}','{$stock_alert_number}','{$ext_property}','{$shipping_fee}','{$free_shipping}',
				'{$commission_template_id}','{$commissions}','{$sort}','{$status}','{$seller_note}','{$in_shop}',
				'{$purchase_count}','{$sale_method}','".time()."','{$pic}')";
			$this->db->query($sql);
			$id = $this->db->insert_id;
		}
		//关键词
		if ($keywords) {
			$sql = "INSERT INTO {$this->tbp}goods_keyword (keyword, goods_id) VALUES ('{$keywords}','{$id}')";
			$this->db->query($sql);
		}
		//更新产品规格表
		$sql = "DELETE FROM {$this->tbp}goods_spec WHERE goods_id='{$id}'";
		$this->db->query($sql);
		if (is_array($spec)) {
			$sql = "INSERT INTO {$this->tbp}goods_spec (goods_id, spec, stocks, price, promote_price) VALUES ";
			foreach ($spec as $k => $sp) {
				if (!strlen($sp)) continue;
				$st = '';
				if ($sto) $st = (isset($sto[$k]) && trim($sto[$k])) ? trim($sto[$k]) : '';
				$pi = 0;
				if ($pri) $pi = (isset($pri[$k]) && floatval($pri[$k])) ? floatval($pri[$k]) : $price;
				$po = 0;
				if ($pro) $po = (isset($pro[$k]) && floatval($pro[$k])) ? floatval($pro[$k]) : 0;
				$sql .= " ('{$id}', '{$sp}', '{$st}', '{$pi}', '{$po}'),";
			}
			$sql = trim($sql, ',');
			$this->db->query($sql);
		}
		//更新图片表
		$sql = "DELETE FROM {$this->tbp}goods_pic WHERE goods_id='{$id}'";
		$this->db->query($sql);
		if (is_array($pics)) {
			$sql = "INSERT INTO {$this->tbp}goods_pic (goods_id, pic, video, memo, add_time) VALUES ";
			foreach ($pics as $k => $pic) {
				if (!strlen($pic)) continue;
				$video = '';
				if ($videos) $video = (isset($videos[$k]) && trim($videos[$k])) ? trim($videos[$k]) : '';
				$memo = '';
				if ($memos) $memo = (isset($memos[$k]) && trim($memos[$k])) ? trim($memos[$k]) : '';
				$sql .= "('{$id}', '{$pic}', '{$video}', '{$memo}', '".time()."'),";
			}
			$sql = trim($sql, ',');
			$this->db->query($sql);
		}
		if ($oldwhere) $oldwhere = urldecode($oldwhere);
		if ($BRSR > 1) $BRSR = ($BRSR - 1)*10;
		if ($BRSR == 1) $BRSR = '';
		header("Location:?app=goods&act=index&{$oldwhere}&BRSR={$BRSR}");
		exit;
	}
	
	//删除
	public function delete() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		$BRSR = (isset($_GET['current_page']) && trim($_GET['current_page'])) ? intval($_GET['current_page']) : 0;
		$oldwhere = (isset($_GET['oldwhere']) && trim($_GET['oldwhere'])) ? trim($_GET['oldwhere']) : '';
		if ($BRSR > 1) $BRSR = ($BRSR - 1)*10;
		if ($BRSR == 1) $BRSR = '';
		$sql = "DELETE FROM {$this->tbp}goods WHERE id='{$id}'";
		if ($this->db->query($sql)) {
			//删除该商品产品规格表
			$sql = "DELETE FROM {$this->tbp}goods_spec WHERE goods_id='{$id}'";
			$this->db->query($sql);
			//删除该商品被添加的购物车
			$sql = "DELETE FROM {$this->tbp}cart WHERE goods_id='{$id}'";
			$this->db->query($sql);
			//删除该商品的收藏
			$sql = "DELETE FROM {$this->tbp}favorite WHERE item_id='{$id}' AND type_id = 1";
			$this->db->query($sql);
			header("Location:?app=goods&act=index&{$oldwhere}&BRSR={$BRSR}");
		} else {
			script('删除失败', '?app=goods&act=index&BRSR={$current_page}&{$oldwhere}');
		}
	}
	
	public function choose(){
        $where = '';
        $id = (isset($_GET['id'])) ? $_GET['id'] : '';
        $status = (isset($_GET['status'])) ? trim($_GET['status']) : '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';

        if ($keyword) {
            $where .= "AND (c.name LIKE '%{$keyword}%' OR c.price LIKE '%{$keyword}%' OR c.keywords LIKE '%{$keyword}%')";
        }
        if($id){
        	$where .="AND id = {$id}";
        }
        if($status){
        	$where .="AND status = {$status}";
        }
        //分页
        $this->setpages(array( 'keyword'=>$keyword,'id'=>$id,'status'=>$status));

        $sql = "SELECT c.*  FROM {$this->tbp}goods c
			WHERE 1 {$where} ORDER BY c.id DESC";
        //echo $sql;
        $rs = $this->ezr->get_results($sql);
        $nav = $this->ezr->get_navigation();
        $this->smarty->assign('rs',$rs);
        $this->smarty->assign('keyword',$keyword);
        $this->smarty->assign('status',$status);
        $this->smarty->assign('page',$nav);
        $this->smarty->assign('id',$id);
        $this->display();
    }
	
    public function goods_qrcode(){
    	$where = '';
		//分页
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
        $shop_id = (isset($_GET['shop_id']) && trim($_GET['shop_id'])) ? intval($_GET['shop_id']) : 0;
        if ($keyword) {
            $where .= "AND (g.name LIKE '%{$keyword}%' OR g.description LIKE '%{$keyword}%' OR g.content LIKE '%{$keyword}%')";
        }
        if ($id) {
            $where .= "AND g.id='{$id}'";
        }
        //分页
        $this->setpages(array('id'=>$id, 'keyword'=>$keyword ,'shop_id'=>$shop_id));
		$sql = "SELECT g.* FROM {$this->tbp}goods g WHERE 1 {$where} ORDER BY g.id DESC";
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		if ($rs) {
			foreach ($rs as $key => $val) {
				$goods_id = $val['id'];
				$url = "{$this->domain}/wap.php?app=goods&act=detail&shop_id={$shop_id}&goods_id={$goods_id}&reseller=";
				$url = urlencode($url);
				$rs[$key]['url'] = $url;
			}
		}
		$sql = "SELECT id, name FROM {$this->tbp}shop ORDER BY id ASC";
		$shops = $this->db->get_results($sql);
		$this->smarty->assign('shops',$shops);
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('shop_id',$shop_id);
		$this->smarty->assign('where',$where);
    	$this->display();
    }
	
    //改变分类
    public function check_cate(){
    	$checkbox = isset($_POST['checkbox']) ? $_POST['checkbox'] : '';
    	$price_type = (isset($_POST['price_type']) && trim($_POST['price_type'])) ? intval($_POST['price_type']) : 0;
    	$price = (isset($_POST['price']) && floatval($_POST['price'])) ? floatval($_POST['price']) : 0;
    	$integral_type = (isset($_POST['integral_type']) && trim($_POST['integral_type'])) ? intval($_POST['integral_type']) : 0;
    	$integral = (isset($_POST['integral']) && intval($_POST['integral'])) ? intval($_POST['integral']) : 0;
    	$ext_property = isset($_POST['ext_property']) ? $_POST['ext_property'] : '';
    	$catechange = (isset($_POST['catechange']) && trim($_POST['catechange'])) ? intval($_POST['catechange']) : 0;
    	$brandchange = (isset($_POST['brandchange']) && trim($_POST['brandchange'])) ? intval($_POST['brandchange']) : 0;
    	if ($checkbox) {
			$ids = implode(',', $checkbox);
			if ($price>0) {
				if ($price_type==0) {
					$sql = "UPDATE {$this->tbp}goods SET price='{$price}' WHERE id IN ({$ids})";
					$this->db->query($sql);
					$sql = "UPDATE {$this->tbp}goods_spec SET price='{$price}' WHERE goods_id IN ({$ids})";
					$this->db->query($sql);
				} else {
					$price /= 100;
					foreach ($checkbox as $k=>$id) {
						$sql = "UPDATE {$this->tbp}goods SET price=price*{$price} WHERE id='{$id}'";
						$this->db->query($sql);
						$sql = "UPDATE {$this->tbp}goods_spec SET price=price*{$price} WHERE goods_id='{$id}'";
						$this->db->query($sql);
					}
				}
			}
			if ($integral>0) {
				if ($integral_type==0) {
					$sql = "UPDATE {$this->tbp}goods SET integral='{$integral}' WHERE id IN ({$ids})";
					$this->db->query($sql);
				} else {
					$integral /= 100;
					foreach ($checkbox as $k=>$id) {
						$sql = "UPDATE {$this->tbp}goods SET integral=FLOOR(integral*{$integral}) WHERE id='{$id}'";
						$this->db->query($sql);
					}
				}
			}
			if (is_array($ext_property)) {
				$ext_property = implode(',', $ext_property);
				$sql = "UPDATE {$this->tbp}goods SET ext_property='{$ext_property}' WHERE id IN ({$ids})";
				$this->db->query($sql);
			}
			if ($catechange>0) {
				$sql = "UPDATE {$this->tbp}goods SET category_id='{$catechange}' WHERE id IN ({$ids})";
				$this->db->query($sql);
			}
			if ($brandchange>0) {
				$sql = "UPDATE {$this->tbp}goods SET brand_id='{$brandchange}' WHERE id IN ({$ids})";
				$this->db->query($sql);
			}
    	}
    	header("Location:?app=goods&act=index");
		exit;
    }
	
    public function qrcode_out(){
    	set_time_limit(600);
		$where ='';
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$shop_id = (isset($_GET['shop_id']) && trim($_GET['shop_id'])) ? trim($_GET['shop_id']) : '';
		if ($keyword) {
            $where .= "AND (g.name LIKE '%{$keyword}%' OR g.huohao LIKE '%{$keyword}%'
            	OR g.description LIKE '%{$keyword}%' OR g.content LIKE '%{$keyword}%')";
        }
        if ($id) {
            $where .= "AND g.id='{$id}'";
        }
		if($where || $shop_id){
		$sql = "SELECT name FROM {$this->tbp}shop WHERE id = {$shop_id}";
		$shop_name = $this->db->get_row($sql);
		$sql = "SELECT g.id ,g.name FROM {$this->tbp}goods g WHERE 1 {$where} ORDER BY g.id DESC";
		$row = $this->db->get_results($sql);
		//创建目录
		$path = ROOT_PATH.UPLOAD_PATH.'/qrcode/'.time().rand(000,999);
		mkdir($path,$mode = 0777);
		foreach ($row as $val) {
			$goods_id = $val->id;
			$url = "{$this->domain}/wap.php?app=goods&act=detail&shop_id={$shop_id}&goods_id={$goods_id}&reseller=";
			$url = urldecode($url);
			$goods_name=$val->name;
			$name= $shop_name->name.$goods_name;
			$name =$this->replace_specialChar($name);
			$name = iconv('UTF-8', 'GB2312//IGNORE', $name);
			$this->qr_out($url,$name,$path);

		}
		require_once (FRAMEWORK_PATH .'/class/phpqrcode/PHPZip.php');
		// $path =ROOT_PATH.UPLOAD_PATH.'/qrcode';
		$zip = new PHPZip();
		//压缩并下载并
		$zip->ZipAndDownload($path);
		//删除文件夹
		rmdir($path);
		}else{
			echo "<meta charset='UTF-8'><script>alert('请填入搜索条件！');</script>";
		}
	}
	public function qr_out($url,$name,$path){
		require_once (FRAMEWORK_PATH .'/class/phpqrcode/phpqrcode.php');
		return QRcode::pngg($url,$name,$path,$outfile=TRUE,$saveandprint=TRUE);
	}
    public function qrcode(){
		require_once (FRAMEWORK_PATH .'/class/phpqrcode/phpqrcode.php');
		$url = urldecode($_GET["url"]);
		return QRcode::png($url);
	}
	//删除字符串中的特殊字符
	public function replace_specialChar($strParam){
    	$regex = "/\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\'|\`|\-|\=|\\\|\|\、|\、|\s|/";
    	return preg_replace($regex,"",$strParam);
	}
}
